package com.youlu.campus.admin.wechat;

import com.youlu.campus.common.base.QResult;
import com.youlu.campus.common.exception.BusinessException;
import com.youlu.campus.wechat.entity.SharingAccountInfo;
import com.youlu.campus.wechat.entity.SharingAccountInfoQueryVo;
import com.youlu.campus.wechat.service.SharingAccountInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 分账账户信息 Controller
 */
@RestController
@RequestMapping("/wechat/sharing-account/")
public class SharingAccountInfoController {

    @Autowired
    private SharingAccountInfoService sharingAccountInfoService;

    /**
     * 创建/修改分账账户信息
     */
    @PostMapping("/save")
    public QResult<SharingAccountInfo> save(@RequestBody @Valid SharingAccountInfo sharingAccountInfo) {
        SharingAccountInfo savedInfo = sharingAccountInfoService.save(sharingAccountInfo);
        return QResult.success(savedInfo);
    }

    /**
     * 根据ID查询分账账户信息
     */
    @GetMapping("/info")
    public QResult<SharingAccountInfo> getById(@RequestParam String id) {
        SharingAccountInfo account = sharingAccountInfoService.findById(id);
        return QResult.success(account);
    }

    /**
     * 删除分账账户信息
     */
    @PostMapping("/delete")
    public QResult<String> delete(@RequestBody SharingAccountInfo sharingAccountInfo) {
        sharingAccountInfoService.deleteById(sharingAccountInfo.getId());
        return QResult.success(sharingAccountInfo.getId());
    }

    /**
     * 分页查询分账账户信息
     */
    @PostMapping("/list")
    public QResult<Page<SharingAccountInfo>> list(@RequestBody SharingAccountInfoQueryVo paramVo) {
        Page<SharingAccountInfo> page = sharingAccountInfoService.findByPage(paramVo);
        return QResult.success(page);
    }

    /**
     * 通过merchatId 查询已添加分账接收方列表
     */
    @PostMapping("/list-mchid")
    public QResult<Page<SharingAccountInfo>> findByMerchantId(@RequestBody SharingAccountInfoQueryVo paramVo) {
        if(StringUtils.isBlank(paramVo.getMerchantId())){
            throw new BusinessException("商户id不能为null");
        }
        return QResult.success(sharingAccountInfoService.findByMerchantId(paramVo));
    }
}